<?php
namespace Emos\Db;

class MySqli extends MySql { 
    public function connect($params) {                
        $params += array(
            'host'      => 'localhost',
            'charset'   => 'utf8',
            'user'      => 'root',
            'password'  => '',
            'post'      => 3306
        );       
  
        $this->connection = new \mysqli($params['host'], $params['user'], $params['password'], $params['database'], $params['post']);        
        if($this->connection->connect_errno) {
            throw new Exception("Can not connect to database '".@$params['database']."': {$this->connection->connect_error}");            
        }        
        $this->connection->query('SET CHARACTER SET '.$params['charset']);        
    }    
    
    function selectQuery($sql) {
        $data = array();
		$res = $this->connection->query($sql);
        if($this->connection->errno !== 0) {
            throw new Exception("SQL query error: {$sql}", $sql);            
        }         
        
        while($rec = $res->fetch_assoc()) {
            $data[] = $rec;
        }
        $res->free();
        return $data;        
    }
    
    function execQuery($sql) {        
		$this->connection->query($sql);
        if($this->connection->errno !== 0) {
            throw new Exception($this->connection->error, $sql);
        }
        return $this->connection->affected_rows;
    }    
    
    function getInsertId() {
        return $this->connection->insert_id;
    }
}
